package team.fourth.combat.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.fourth.combat.commen.entity.Result;
import team.fourth.combat.dao.MemberDao;
import team.fourth.combat.dao.OrderDao;
import team.fourth.combat.dao.OrderSettingMapper;
import team.fourth.combat.pojo.Member;
import team.fourth.combat.pojo.Order;
import team.fourth.combat.pojo.OrderInfoVo;
import team.fourth.combat.pojo.OrderSetting;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderSettingMapper orderSettingMapper;

    @Autowired
    OrderDao orderDao;

    @Autowired
    MemberDao memberDao;

    /**
     * synchronized 悲观锁
     * 基于version字段             乐观锁（无锁）
     * @param orderInfoVo
     * @return
     */
    @Override
    public  Result submit(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String name = orderInfoVo.getName();
        String idCard = orderInfoVo.getIdCard();
        Integer sex = orderInfoVo.getSex();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();

        OrderSetting orderSetting = orderSettingMapper.findByDate(orderDate);
       if(orderSetting == null){
           return Result.error("没有档期");//提示产品给
       }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(number <= reservations){
            //线程1 100 99 0
            //线程2 100 99 0
            //线程3 100 99 0
            return Result.error("预满");
        }

        Member member = memberDao.findByTelephone(telephone);
        if(null == member){
            member = new Member();
            member.setName(name);
            member.setSex(String.valueOf(sex));
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        Integer memberId = member.getId();
        Order queryParam = new Order();
        queryParam.setOrderDate(orderDate);
        queryParam.setSetmealId(setmealId);
        queryParam.setMemberId(memberId);
        List<Order> orders = orderDao.findByCondition(queryParam);
        if(CollectionUtil.isNotEmpty(orders)){
            return Result.error("不要重复预约");
        }
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(orderType);
        order.setSetmealId(setmealId);
        orderDao.add(order);

        orderSetting.setReservations(orderSetting.getReservations() + 1);
        int i = orderSettingMapper.editReservations(orderSetting);
        if(i == 0){
            throw new RuntimeException("");
        }
        return Result.success("",order);//要返回order对象，页面用到了订单id
    }
}
